$(function(){
	
	//初始化
	prescriptionDetail();
	userAdress();
	
//	药品列表渲染
	function prescriptionDetail(){
		var orderid = utilModule.getQueryString("id");
		var param = {"id":orderid};
		function callBackSlider(data){
			console.log(data);
			var html ='';
			var html2 ='';
			for(i in data.prescriptionDetailList){
				html+='<li><div><img src="'+data.prescriptionDetailList[i].thumbnail+'" /><span>'+data.prescriptionDetailList[i].name+'<p>*'+data.prescriptionDetailList[i].count+'</p></span><font>￥'+data.prescriptionDetailList[i].price+'</font></div></li>';
				html2+='<li><div><img src="'+data.prescriptionDetailList[i].thumbnail+'" /><span>'+data.prescriptionDetailList[i].name+'<p>*'+data.prescriptionDetailList[i].count+'</p></span></div></li>';
			}
			$("#item1 .drug-listContent ul").empty();
			$("#item1 .drug-listContent ul").append(html);
			$("#item2 .drug-listContent ul").empty();
			$("#item2 .drug-listContent ul").append(html2);
			var moneyAll=parseFloat(data.totlmoney);
			moneyAll+=parseFloat(10);
			$("#item1 .drug-listContent .all span").text("￥"+data.totlmoney);
			$("#item1 .drug-listContent .allM").text("￥"+moneyAll);
			$("#item1 .drug-allMoney span").text("￥"+data.totlmoney);
			$("#item2 .drug-allMoney").attr("orderId",data.id);
		}
		onlineContinuationModule.onlineOrderDetail(param,callBackSlider);
	}
//	收货地址查询
	function userAdress(){
		var addr_id = JSON.parse(sessionStorage.getItem("adressId"));
		if(addr_id == null){
			var param = {"userId":USERID,"isDefault":1};
		}else{
			var param = {"id":addr_id};
		}
		function callBackSlider(data){
			console.log(data);
			if(data.result==""){
				$("#item1 .adressBottom").show();
				$("#item1 .mui-table-view-cell").hide();
			}else{
				$(".getaddrId").data("addrid",data[0].id);
				$("#item1 .adressBottom").hide();
				$("#item1 .mui-table-view-cell").show();
				
				var html = data[0].consignee+'<span>'+data[0].mobile+'</span><p class="mui-ellipsis">'+data[0].address+data[0].houseNumber+'</p>';
				$("#item1 .mui-navigate-right").append(html);
			}
		}
		onlineContinuationModule.userAdressSelect(param,callBackSlider);
	}
	
	var orderid=$("#item2 .drug-allMoney").attr("orderId");
	$("#item1 .adressBottom").on("click",function(){
		var param = {"userId":USERID};
		function callBackSlider(data){
			if(data.length>0){
				location.href = "../../html/onlineContinuation/receiving-list.html?orderid="+orderid;
			}else{
				location.href = "../../html/onlineContinuation/receiving-address.html?orderid="+orderid;
			}
		}
		onlineContinuationModule.userAdressSelect(param,callBackSlider);
	});
	$("#item1 .mui-table-view-cell").on("click",function(){
		location.href = "../../html/onlineContinuation/receiving-list.html?orderid="+orderid;
	});
	
	$("#item2 .adressBottom").on("click",function(){
		location.href = "../../html/prescription/prescription-wechat.html?orderid="+orderid;
	});
	$("#item2 .drug-allMoney font").on("click",function(){
		location.href = "../../html/prescription/prescription-wechat.html?orderid="+orderid;
	});
	
	$("#medicalSubmit").on("click",function(){
		var addrId = JSON.parse(sessionStorage.getItem("adressId"));
		if(addrId!=null){
			var param = {id:orderid,openId:OPENID,addrId:addrId}
		}else{
			addrId = $(".getaddrId").data("addrid");
			if(addrId==undefined){
				mui.alert("收获地址不能为空","提示")
				return;
			}else{
				var param = {id:orderid,openId:OPENID,addrId:addrId}
			}
		}
		onlineContinuationModule.userMedicalSubmit(param,callBackSlider);
		function callBackSlider(data){
			console.log(data);
			mui.alert("已提交","提示",function(){
				location.replace("../../html/order/order-pay.html?orderType=5&orderid="+data.id);
			}); 
		}
	});
	
	$(".adressMap .mapHead font").on("click",function(){
		var orderid = utilModule.getQueryString("id");
		location.href = "../../html/prescription/map-more.html?orderid="+orderid;
	});
	
	
	//获取ApiSign
	var orderId = utilModule.getQueryString("id");
	function initGetJsApiSign(url) {
		var params = {
			"url" : url
		};
		userCenterModule.getJsApiSign(params, function(callback) {
			var info = callback;
			if (info != null) {
				wx.config({
					debug : false,  /*正常模式*/   // true开启调试模式 //  
					appId : info.appId,
					timestamp : info.timestamp, // 必填，生成签名的时间戳
					nonceStr : info.nonceStr, // 必填，生成签名的随机串
					signature : info.signature, // 必填，签名，见附录1
					jsApiList : [ 'scanQRCode', 'getLocation']
				});
			}
		});
	}
       
	initGetJsApiSign("n/html/onlineContinuation/drug-delivery.html?id="+orderId);
	
	wx.ready(function(){ 
	   wx.getLocation({
	    	success: function(res) {
	    		console.log("###############"+res);
		        var latitude = res.latitude; // 纬度，浮点数，范围为90 ~ -90
		        var longitude = res.longitude; // 经度，浮点数，范围为180 ~ -180。
		        //去数据库查询获取附近的门店
	            utilModule.get({
	                url: "prescription/getBestPharmacy",
	                data: {"latitude":latitude,"longitude":longitude},
	                needJson: false, 
	                async: false,
	                success: function (response) { 
                		response=response[0]
                    	var url = "http://apis.map.qq.com/tools/poimarker?type=0&marker=coord:"+response.lat+","+response.lon+";title:"+response.name+";addr:"+response.address+"&key=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77&referer=myappr=myapp"	
                    	$(".mapDistance span").on('click',function(){
                    		location.href = url;
                    	});
                    	$(".mapHead span").text(response.name);
                    	$(".mapAdress span").text(response.address);
                    	if(response.juli< 1000){
                    		$(".mapDistance font").html(response.juli+"m");
                    	}else if(response.juli >= 1000){
                    		$(".mapDistance font").html((Math.round(response.juli/10)/100).toFixed(2) + "km");
                    	}
					}
	            });
			}
		});
		wx.error(function(res) {
			mui.alert("返回结果：" + res.errMsg);
		});
	});		
});

